package top.ztf.erp.utils;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * mybatis 工具类
 */
public class MyBatisUtil {

    private final static SqlSessionFactory sqlSessionFactory;
    private final static ThreadLocal<SqlSession> factory = new ThreadLocal<>();

    private MyBatisUtil(){}

    static {
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("mybatis-conf.xml");
         sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    }

    public static SqlSession getSqlSession(){
//        SqlSession sqlSession = factory.get();
//        if (sqlSession==null){
//            sqlSession = sqlSessionFactory.openSession();
//            factory.set(sqlSession);
//        }
//        return sqlSession;
        return sqlSessionFactory.openSession();
    }

}
